<?php
    error_reporting(0);
    define('__ROOT__', dirname(dirname(__FILE__)));
    require_once(__ROOT__.'\config\config.php'); 
    require_once(__ROOT__.'\model\db.php');
    require_once(__ROOT__.'\model\bill_detail.php');
    require_once(__ROOT__.'\model\bill.php');
    require_once (__ROOT__.'\model\products.php');
    if(isset($_GET['action']))
    {
        $bill = new Bill();
        $product = new Products();
        $bills = $bill->selectid($_GET['idbill']);
        $costbill = $bills[0]['cost'] - $_GET['totalcost'];   
        $billdetail = new Bill_detail();
        
        $billdetails = $billdetail->selectid($_GET['id']);
        $products = $product->selectname($billdetails[0]['nameproduct']);
        $quantity = $products[0]['quantity'] + $billdetails[0]['quantity'];
        $products = $product->updatename($billdetails[0]['nameproduct'], $quantity);
        $billdetails = $billdetail->delete($_GET['id']);
        if($costbill == 0)
        {
            $bills = $bill->delete($_GET['idbill']);
            echo("<script>location.href = '".HOST."/view/view_admin.php';</script>");
        }
        else
        {
            $bills = $bill->updatecost($costbill, $_GET['idbill']);
        }      
    }
   
    echo("<script>location.href = '".HOST."view/view_billdetail.php?id=".$_GET['idbill']."&status=0';</script>");
?>